package v1

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// @Tags GoodsOrder
// @Summary 创建GoodsOrder
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.GoodsOrder true "创建GoodsOrder"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /goodsOrder/createGoodsOrder [post]
func CreateGoodsOrder(c *gin.Context) {
	var goodsOrder model.GoodsOrder
	_ = c.ShouldBindJSON(&goodsOrder)
	goodsOrder.GoodsOrderOwner = getUserName(c)
	if err := service.CreateGoodsOrder(goodsOrder); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags GoodsOrder
// @Summary 删除GoodsOrder
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.GoodsOrder true "删除GoodsOrder"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /goodsOrder/deleteGoodsOrder [delete]
func DeleteGoodsOrder(c *gin.Context) {
	var goodsOrder model.GoodsOrder
	_ = c.ShouldBindJSON(&goodsOrder)
	if err := service.DeleteGoodsOrder(goodsOrder); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// @Tags GoodsOrder
// @Summary 批量删除GoodsOrder
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除GoodsOrder"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /goodsOrder/deleteGoodsOrderByIds [delete]
func DeleteGoodsOrderByIds(c *gin.Context) {
	var IDS request.IdsReq
    _ = c.ShouldBindJSON(&IDS)
	if err := service.DeleteGoodsOrderByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// @Tags GoodsOrder
// @Summary 更新GoodsOrder
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.GoodsOrder true "更新GoodsOrder"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /goodsOrder/updateGoodsOrder [put]
func UpdateGoodsOrder(c *gin.Context) {
	var goodsOrder model.GoodsOrder
	_ = c.ShouldBindJSON(&goodsOrder)
	if err := service.UpdateGoodsOrder(goodsOrder); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags GoodsOrder
// @Summary 用id查询GoodsOrder
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.GoodsOrder true "用id查询GoodsOrder"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /goodsOrder/findGoodsOrder [get]
func FindGoodsOrder(c *gin.Context) {
	var goodsOrder model.GoodsOrder
	_ = c.ShouldBindQuery(&goodsOrder)
	if err, regoodsOrder := service.GetGoodsOrder(goodsOrder.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"regoodsOrder": regoodsOrder}, c)
	}
}

// @Tags GoodsOrder
// @Summary 分页获取GoodsOrder列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GoodsOrderSearch true "分页获取GoodsOrder列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /goodsOrder/getGoodsOrderList [get]
func GetGoodsOrderList(c *gin.Context) {
	var pageInfo request.GoodsOrderSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := service.GetGoodsOrderInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败", zap.Any("err", err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}

func GetDeliveryGoodsOrderList(c *gin.Context) {
	var pageInfo request.GoodsOrderSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := service.GetDeliveryGoodsOrderInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func GetGoodsOrderById(c *gin.Context) {
	orderID := c.Query("goodsOrderId")
	if err, regoodsOrder := service.GetGoodsOrderInfoById(orderID); err != nil {
		global.GVA_LOG.Error("获取失败", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(gin.H{"regoodsOrder": regoodsOrder}, c)
	}
}

func CreateRelateBill(c *gin.Context) {
	//var billInfo model.BillInfo
	//_ = c.ShouldBindJSON(&billInfo)
	//billInfo.BillAmount = billInfo.OrderAmount + billInfo.DeliveryAmount
	//billInfo.BillOperator = getUserName(c)
	goodsId := c.Query("goodsOrderId")
	if err := service.CreateRelateBill(goodsId); err != nil {
		global.GVA_LOG.Error("创建失败", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

func SetStateStorage(c *gin.Context) {
	goodsOrderId := c.Query("goodsOrderId")   // query只能从get操作中解析出数据
	if err := service.SetStateStorage(goodsOrderId); err != nil {
		global.GVA_LOG.Error("创建失败", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}
